﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace WpfApp.View.SystemInformation
{
    /// <summary>
    /// WD_InsertStaffAccountManage.xaml 的交互逻辑
    /// </summary>
    public partial class WD_InsertStaffAccountManage : Window
    {
        public WD_InsertStaffAccountManage()
        {
            InitializeComponent();
        }
        //实例化业务逻辑层
        BLL.UCStaffAccountManage.UCStaffAccountManageClient myClient = new BLL.UCStaffAccountManage.UCStaffAccountManageClient();

        /// <summary>
        /// 页面加载时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region 下拉框的绑定
            //员工
            DataTable dtStaff = myClient.SelectStaff().Tables[0];
            cbo_Name.ItemsSource = dtStaff.DefaultView;
            cbo_Name.SelectedValuePath = "staffID";//id
            cbo_Name.DisplayMemberPath = "staffName";//name
            #endregion
            #region 权限组的绑定
            DataTable dtPermissionGroup = myClient.SelectPermissionGroup().Tables[0];
            cbo_Group.ItemsSource = dtPermissionGroup.DefaultView;
            cbo_Group.SelectedValuePath = "permissionGroupID";//id
            cbo_Group.DisplayMemberPath = "permissionGroupName";//name
            #endregion
        }
        /// <summary>
        /// 新增员工信息(保存按钮)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断页面数据是否为空
                if(Convert.ToInt32(cbo_Name.SelectedValue)>0&&Convert.ToInt32(cbo_Group.SelectedValue)>0 && txt_Account.Text.Trim()!=string.Empty && PB_Password.Password.Trim() != string.Empty)
                {
                    //获取页面数据
                    int intStaffID = Convert.ToInt32(cbo_Name.SelectedValue);
                    string strAccounts = txt_Account.Text.Trim();
                    int intPGroupID = Convert.ToInt32(cbo_Group.SelectedValue);
                    string strPassword = PB_Password.Password.Trim();
                    bool blEffective = (bool)chk_Effect.IsChecked;
                    string strNote = txt_Note.Text.Trim();
                    /*（3）新增保存
                     * -1 重复
                     *  1 成功
                     */
                    int intCount = myClient.InsertOperator(intPGroupID, intStaffID, strAccounts, strPassword, blEffective, strNote);
                    if (intCount == -1)
                    {
                        MessageBox.Show("账号重复！", "系统提示", MessageBoxButton.OKCancel,
                          MessageBoxImage.Exclamation); //弹出确定对话框
                    }
                    else
                    {
                        MessageBoxResult dr = MessageBox.Show("您新注册了一个账号！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Asterisk); //弹出确定对话框                        
                        this.Close();
                    }

                }
                else
                {
                    MessageBox.Show("请把页面数据填写完整！", "系统提示", MessageBoxButton.OK,
                         MessageBoxImage.Warning); //弹出确定对话框
                }


            }
            catch (Exception)
            {
                MessageBox.Show("数据异常！", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
        /// <summary>
        /// 关闭新增窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult dr = MessageBox.Show("退出页面数据将不保留。", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);//弹出确定对话框
            if (dr == MessageBoxResult.OK)//如果点了确定按钮
            {
                //关闭窗口
                this.Close();
            }
        }
    }
}
